using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using System.Text;
using QLVMBAYDomain;
namespace QLVMBAYDatacess
{
    public class SanBayDA : DataAccessBase
    {
		#region Constructors
		
		public SanBayDA() : base()
        {
			_selectSql = "Select * From SANBAY";
			_insertSql = "Insert Into SANBAY(MASB,MATP,TENSB) Values (@MASB,@MATP,@TENSB)";
        	_updateSql = "Update SANBAY Set MATP=@MATP,TENSB=@TENSB Where 1=1 And MASB=@MASB";
        	_deleteSql = "Delete From SANBAY Where 1=1 And MASB=@MASB";
        	_primarSql = "Select * From SANBAY Where 1=1 And MASB=@MASB";
        }
		
		protected static SanBayDA _inst = null;
		public static SanBayDA Inst
		{
			get
			{
				if(_inst == null)
					_inst = new SanBayDA();
				return _inst;
			}
		}
		
		#endregion
		
        #region Get Methods

		private SanBay GetCurrent(IDataReader reader, bool isDeep)
		{
			SanBay ret = new SanBay();
			ret.MaSanBay = (string)reader["MASB"];
			ret.TenSanBay = (string)reader["TENSB"];
			
			ret.ItemThanhPho = new ThanhPho();
			if(isDeep)
			{
				ret.ItemThanhPho =  ThanhPhoDA.Inst.GetByPrimaryKey(isDeep, (string)reader["MATP"]);
			}
			else
				ret.ItemThanhPho.MaThanhPho = (string)reader["MATP"];
			
			
			return ret;
		}

		public List<SanBay> GetAll(bool isDeep)
		{
			DbCommand comm = GetCommand(_selectSql);
            using (Connection)
            {
                using (DbDataReader dr = comm.ExecuteReader())
                {
                    List<SanBay> ret = new List<SanBay>();
					while(dr.Read())
					{
						ret.Add(GetCurrent(dr, isDeep));
					}
					return ret;
                }
            }
		}
		
		public SanBay GetByPrimaryKey(bool isDeep, params object[] args)
		{
			DbCommand cmd = GetCommand(_primarSql);
			if(args.Length < 1)
			{
				throw new Exception();
			}
            SetParam(cmd, "@MASB", args[0]);

            using (Connection)
            {
                using (DbDataReader dr = cmd.ExecuteReader())
                {
					if(dr.Read())
					{
						return GetCurrent(dr, isDeep);
					}
					return null;
                }
            }
		}
		
		public bool Insert(SanBay entity)
		{
			DbCommand cmd = GetCommand(_insertSql);
            SetParam(cmd, "@MASB", entity.MaSanBay);
            SetParam(cmd, "@TENSB", entity.TenSanBay);

			SetParam(cmd, "@MATP", entity.ItemThanhPho.MaThanhPho);
			
            using (Connection)
            {
				int ret = cmd.ExecuteNonQuery();
				return ret > 0;
            }
		}
		
		public bool Update(SanBay entity)
		{
			DbCommand cmd = GetCommand(_updateSql);
            SetParam(cmd, "@MASB", entity.MaSanBay);
            SetParam(cmd, "@TENSB", entity.TenSanBay);

			SetParam(cmd, "@MATP", entity.ItemThanhPho.MaThanhPho);
			
            using (Connection)
            {
				int ret = cmd.ExecuteNonQuery();
				return ret > 0;
            }
		}
		
		public bool Delete(SanBay entity)
		{
			DbCommand cmd = GetCommand(_deleteSql);
            SetParam(cmd, "@MASB", entity.MaSanBay);

            using (Connection)
            {
				int ret = cmd.ExecuteNonQuery();
				return ret > 0;
            }
		}
		
		#endregion
    }
}


